package com.example.demo03_mvm.viewmodel;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.demo03_mvm.adapter.KfAdapter;
import com.example.demo03_mvm.db.KfEntity;
import com.example.demo03_mvm.repos.KfRepos;

import java.util.List;
import java.util.Random;

import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class KFViewModel extends BaseViewModel{
    KfRepos kfRepos;

    public KFViewModel(@NonNull Application application) {
        super(application);
        kfRepos=new KfRepos();
    }
    String[] msgs={"您好","hello","在的","请稍等"};
    public void sendMsg(KfEntity kf,boolean flag){
        kfRepos.insert(kf, new Action() {
            @Override
            public void run() throws Exception {
                Log.i("===","发送成功");
                if(!flag) {
                    //自动回复
                    int i = new Random().nextInt(msgs.length);
                    KfEntity kfEntity = new KfEntity(msgs[i], 0, kf.getGid());
                    kfRepos.insert(kfEntity, new Action() {
                        @Override
                        public void run() throws Exception {

                        }
                    });
                }
            }
        });
    }

    public void getMsg(KfAdapter kfAdapter,int gid){
        kfRepos.getMsgByGid(gid, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                kfAdapter.getData().clear();
                kfAdapter.getData().addAll((List<KfEntity>)o);
                kfAdapter.notifyDataSetChanged();
            }
        });
    }
}
